<?php

class Admin_Model_Comments{
	
	private $commentTbl;
	
	public function __construct(){
		$this->commentTbl	= new Admin_Model_DbTable_Comments();
	}
	
	public function selectComment($where=NULL, $order='idComment DESC',$limit =NULL){
		$dbAdapter 	= Zend_Db_Table::getDefaultAdapter();
		
		$dbSelect 	= new Zend_Db_Select($dbAdapter);
		$dbSelect->from(array('cm'=> 'comments'),array('idComment','subject','content','datecomment'))
				->joinLeft(array('u'=>'users'), 'u.username = cm.username','*');
		if ($where != null) $dbSelect->where($where);
		$dbSelect->order($order);
		if ($limit != null) $dbSelect->limit($limit);

		return $dbSelect;
	}
	
	public function deleteComment($idComment){
		$this->commentTbl->delete("idComment =".$idComment);
	}
	
	public function fetchAllComments($where=NULL){
		if ($where !=NULL) {
			return $this->commentTbl->fetchAll($where)->toArray();
		}else {
			return $this->commentTbl->fetchAll()->toArray();
			
		}
		
	}
	
	public function addComment($username, $subject, $content,$dateComment){
		$data =array(
			'username'		=> $username,
			'subject'		=> $subject,
			'content'		=> $content,
			'datecomment'	=> $dateComment 
		);
		
		$this->commentTbl->insert($data);
	}


}

